from glob import glob
import os

def rglob(dirname, pat, incl_dirs=False):
    """recursive glob, gets all files that match the pattern within the directory tree"""
    fns = glob(os.path.join(dirname, pat))
    dirs = os.listdir(dirname)
    if len(dirs) > 0:
        dirs = [os.path.join(dirname, fn) for fn in dirs]
        if len(dirs) > 0:
            dirs = [fn for fn in dirs if os.path.isdir(fn)]
    if incl_dirs==True:
        fns += dirs
    for d in dirs:
        fns += rglob(d, pat)
    fns.sort()
    return fns
